/**
 * @param {string} s
 * @return {string}
 */
var removeOuterParentheses = function(s) {
  let needStack = [], stack = [], left = 0, start = -1, end = -1;
  for(let i = 0; i < s.length; i++) {
    if(s[i] == '(') {
      left++;
      if(left == 1) {
        start = i;
      }
    } else {
      left--;
      if(left == 0) {
        end = i;
        // console.log(start, end);
        if(start + 1 !== end) {
          needStack.push(s.slice(start + 1, end));
        }
      }
    }
  }
  return needStack.join("");
}
let s = "(()())(())";
console.log(removeOuterParentheses(s));
s = "(()())(())(()(()))";
console.log(removeOuterParentheses(s));
s = "()()";
console.log(removeOuterParentheses(s));
